import { ref } from "vue";
import { getHistoryInfoApi } from "@/api/configCenter/warehouseEntryChannel";

export default function InitHook(state: any) {
  const { passData } = state;
  const formState = ref<Base.GeneraDataType>({});

  const loading = ref<boolean>(false);

  const init = () => {
    loading.value = true;
    getHistoryInfoApi({ channelId: passData.value.channelId, quotationHistoryId: passData.value.id })
      .then((res: any) => {
        const { code, data } = res;
        if (code === 200) {
          formState.value = data;
          formState.value.itemList = formState.value.itemList.map((t: any) => {
            return {
              ...t,
              countryList: t.countryList.map((q: any) => {
                return q.countryId;
              })
            };
          });
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };
  return {
    formState,
    loading,
    init
  };
}
